<?php
/**
 *	Logic of User, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		CMF_Krypton_Core_Logic
 *	@uses			CMF_Krypton_Exception_Logic
 *	@uses			Logic_Authentication
 *	@uses			Logic_User
 *	@uses			Model_Role
 *	@uses			Model_RoleUser
 *	@uses			Model_RoleRight
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
/**
 *	Logic of User, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		CMF_Krypton_Core_Logic
 *	@uses			CMF_Krypton_Exception_Logic
 *	@uses			Logic_Authentication
 *	@uses			Logic_User
 *	@uses			Model_Role
 *	@uses			Model_RoleUser
 *	@uses			Model_RoleRight
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
 class Logic_Role extends CMF_Krypton_Core_Logic
 {
	/**
	 *	Adds a new user into the database after verifying an unique name and email.
	 *	@access		private
	 *	@param		array		$data				Array of User Data
	 *	@param		string		$definitionFile		Definition File for Validation
	 *	@param		string		$formName			Form Name in Definition File
	 *	@param		string		$prefix				Prefix of Input Values
	 *	@return		bool
	 */
	public function addRole( $data, $definitionFile = "", $formName = "", $prefix = "" )
	{
		import( 'de.ceus-media.framework.krypton.exception.Logic' );
		$model	= new Model_Role();

		//  --  VALIDATION  --  //
		$data	= $this->removePrefixFromFields( $data, $prefix );
		$data['creatorId']	= Logic_Authentication::getCurrentUser();
		if( $definitionFile && $formName )
			$this->validateForm( $definitionFile, $formName, $data, $prefix );

		//  --  CHECK UNIQUE TITLE  --  //
		$model->focusIndex( 'title', $data['title'] );
		if( count( $model->get() ) > 0 )
			throw new CMF_Krypton_Exception_Logic( 'error_role_already_existing', $data['title'] );

		//  --  ADD ROLE  --  //
		$roleId	= $model->insert( $data );
		return $roleId;
	}

	/**
	 *	Indicates whether a Role Right is set.
	 *	@access		public
	 *	@param		int			$roleId			Role ID
	 *	@param		int			$objectId		Right Object ID
	 *	@param		int			$actionId		Right Action ID
	 *	@return		bool
	 */
	public static function addRoleUser( $roleId, $userId )
	{
		$model	= new Model_RoleUser();
		$data	= array(
			'roleId'	=> $roleId,
			'userId'	=> $userId,
			'creatorId'	=> Logic_Authentication::getCurrentUser(),
			'assigned'	=> time(),
			);
		return $model->insert( $data );
	}

	/**
	 *	Edits User Data.
	 *	@access		public
	 *	@param		int			$roleId				Role ID
	 *	@param		array		$data				Data to update
	 *	@param		string		$definitionFile		Definition File for Validation
	 *	@param		string		$formName			Form Name in Definition File
	 *	@param		string		$prefix				Prefix of Input Values
	 *	@return		bool
	 */
	public function editRole( $roleId, $data, $definitionFile, $formName, $prefix = "" )
	{
		import( 'de.ceus-media.framework.krypton.exception.Logic' );
		$model	= new Model_Role( $roleId );

		//  --  VALIDATION  --  //
		$data	= $this->removePrefixFromFields( $data, $prefix );
		if( $definitionFile && $formName )
			$this->validateForm( $definitionFile, $formName, $data, $prefix );

		//  --  CHECK ROLE ID  --  //
		if( !$model->exists() )
			throw new LogicException( 'error_role_not_existing' );

		//  --  CHECK UNIQUE TITLE  --  //
		$roles	= $model->find( array(), array( 'title' => $data['title'], 'roleId' => "!=".$roleId ) );
		if( count( $roles ) )
			throw new CMF_Krypton_Exception_Logic( 'error_role_already_existing', $data['title'] );

		return $model->update( $data );
	}

	/**
	 *	Returns Role data by Role ID.
	 *	@access		public
	 *	@param		int			$roleId			Role ID
	 *	@return		array						Role data
	 */
	public static function getRoleData( $roleId )
	{
		$model	= new Model_Role( $roleId );
		if ( !$model->exists() )
		{
			throw new LogicException( 'error_role_invalid' );
		}
		return $model->get();
	}

	/**
	 *	Returns Role ID from Role Title.
	 *	@access		public
	 *	@param		string		$title			Role Title
	 *	@return		int
	 */
	public static function getRoleIdFromTitle( $title )
	{
		$model	= new Model_Role();
		$model->focusIndex( 'title', $title );
		$data	= $model->get();
		if( $data )
			return $data['roleId'];
		return 0;
	}

	/**
	 *	Returns all Pages assigned  to a Role.
	 *	@access		public
	 *	@param		int			$roleId			Role ID
	 *	@return		array
	 */
	public static function getRolePages( $roleId )
	{
		$controller	= CMF_Krypton_Core_Registry::getStatic( 'controller' );
		$role		= strtolower( str_replace( " ", "_", self::getRoleTitle( $roleId ) ) );
		$list		= array();
		$pagesets	= $controller->getPages();
		foreach( $pagesets as $scope => $pages )
			foreach( $pages as $pageLink => $pageData )
				if( in_array( $role, $pageData['roles'] ) )
					$list[] = $pageLink;
		return $list;
	}

	/**
	 *	Returns Roles.
	 *	@access		public
	 *	@param		bool			$full				Flag: return full information, otherwise array(ID => Title)
	 *	@param		string			$likeTitle			Title to look for
	 *	@param		string			$orderBy			Column to order by
	 *	@param		string			$orderDirection		Order Direction (ASC|DESC)
	 */
	public static function getRoles( $full = FALSE, $likeTitle = FALSE, $orderBy = FALSE, $orderDirection = FALSE )
	{
		$model		= new Model_Role();
		$conditions	= array();
		$orders		= array();
		if( $likeTitle )
			$conditions['title'] = "%".$likeTitle."%";
		if( $orderBy && $orderDirection )
			$orders	= array( $orderBy => $orderDirection );
		$roles	= $model->find( $conditions, $orders );
		if( $full )
			return $roles;
		$list	= array();
		foreach( $roles as $role )
			$list[$role['roleId']]	= $role['title'];
		return $list;
	}

 	/**
	 *	Returns Role Title by Role ID.
	 *	@access		public
	 *	@param		int			$roleId			Role ID
	 *	@return		string
	 */
	public static function getRoleTitle( $roleId )
	{
		$data	= self::getRoleData( $roleId );
		return $data['title'];
	}

	/**
	 *	Returns Users of Role.
	 *	@access		public
	 *	@param		int			$roleId			Role ID
	 *	@return		array
	 */
	public static function getRoleUsers( $roleId )
	{
		$model	= new Model_RoleUser();
		$model->focusIndex( 'roleId', $roleId );
		$users	= $model->get( FALSE );
		$list	= array();
		foreach( $users as $user )
			$list[$user['userId']]	= Logic_User::getUserName( $user['userId'] );
		return $list;
	}

 	/**
	 *	Returns Role of User.
	 *	@access		public
	 *	@param		int			$userId			User ID
	 *	@return		string
	 */
	public static function getUserRoles( $full = FALSE, $userId, $orderBy = FALSE, $orderDirection = FALSE, $limit = FALSE, $offet = FALSE )
	{
		$model	= new Model_RoleUser();
		$model->focusIndex( 'userId', $userId );
		$orders	= array();
		if( $orderBy && $orderDirection )
			$orders	= array( $orderBy => $orderDirection );
		$limits	= array();
		if( $limit && $offet )
			$limits	= array( $limit, $offet );
		$relations	= $model->get( FALSE, $orders, $limits );
		$list		= array();
		foreach( $relations as $relation )
			if( $full )
				$list[]	= self::getRoleData( $relation['roleId'] );
			else
				$list[$relation['roleId']]	= self::getRoleTitle( $relation['roleId'] );
		return $list;
	}

	/**
	 *	Removes a Role, its Rights and Relations to Users.
	 *	@access		public
	 *	@param		int			$roleId			Role ID
	 *	@return		bool
	 */
	public static function removeRole( $roleId )
	{
		$model	= new Model_Role( $roleId );
		if( !$model->exists() )
			throw new LogicException( 'error_role_not_existing' );
		self::removeRoleUsers( $roleId );
		return (bool) $model->delete();
	}

	/**
	 *	Removes a Role from an User.
	 *	@access		public
	 *	@param		int			$roleId			Role ID
	 *	@param		int			$userId			User ID
	 *	@return		bool
	 */
	public static function removeRoleUser( $roleId, $userId )
	{
		$model	= new Model_RoleUser();
		$model->focusIndex( 'roleId', $roleId );
		$model->focusIndex( 'userId', $userId );
		return (bool) $model->delete();
	}

	/**
	 *	Removes a Role from all Users and returns amount of affected Users.
	 *	@access		public
	 *	@param		int			$roleId			RoleId
	 *	@return		int
	 */
	public static function removeRoleUsers( $roleId )
	{
		$model	= new Model_RoleUser();
		$model->focusIndex( 'roleId', $roleId );
		return $model->delete();
	}

	/**
	 *	Removes all Roles of an User and returns amount of removed Roles.
	 *	@access		public
	 *	@param		int			$userId			User ID
	 *	@return		ints
	 */
	public static function removeUserRoles( $userId )
	{
		$model	= new Model_RoleUser();
		$model->focusForeign( 'userId', $userId );
		return $model->delete();
	}
}
?>